<?php $codeSamples = new RAML2HTML\codeSamples($RAML); ?>
<br />

<span class="badge badge_<?php echo $RAML->getCurrentAction(); ?>"><?php echo $RAML->getCurrentAction(); ?></span>
<strong style="display: inline-block; font-size: 14px; padding-top: 5px;" class="text_<?php echo $RAML->getCurrentAction(); ?>"><?php echo $RAML->baseUri . $RAML->getCurrentPath(); ?></strong>

<br />

<p style="margin-top: 30px;"><?php echo RAML2HTML\markdown::parse($RAML->action()->get('description')); ?></p>


<?php if ($RAML->action()->get('queryParameters')->isArray()): ?>
	<?php if ($tryIt) { ?>
	<a href="javascript:void(0);" class="badge" onClick="showTryIt();" style="margin-top: 16px; margin-right: 4%; float: right; background: #333;">Try It</a>
	<?php } ?>
	<h3>请求参数</h3>
	<table>
		<thead>
			<tr>
				<td>参数</td><td>类型</td><td>是否必填</td><td>描述</td>
			</tr>
		</thead>

		<?php
		foreach ($RAML->action()->get('queryParameters')->toArray() as $param => $details) {
		?>

			<tr>
				<td><?php echo $param; ?></td><td><?php echo $details['type']; ?></td><td><?php echo (isset($details['required']) && $details['required'] == 1 ? 'Yes' : ''); ?></td><td><?php echo $details['description']; ?><?php echo (isset($details['example']) ? '<span style="display: block; margin: 6px 0 8px 0; border-top: 1px solid #aaa; color: #666; padding: 4px; font-size: 10px;"><b>Example:</b> ' . $details['example'] . '</span>' : ''); ?></td>
			</tr>

		<?php } ?>

	</table>
<?php endif; ?>


<?php if ($RAML->action()->get('body')->get('application/x-www-form-urlencoded')->get('formParameters')->isArray()): ?>
<h3>表单参数</h3>
<table>
	<thead>
		<tr>
			<td>参数</td><td>类型</td><td>是否必填</td><td>描述</td>
		</tr>
	</thead>

	<?php
	foreach ($RAML->action()->get('body')->get('application/x-www-form-urlencoded')->get('formParameters')->toArray() as $param => $details) {
	?>

		<tr>
			<td><?php echo $param; ?></td><td><?php echo $details['type']; ?></td><td><?php echo (isset($details['required']) && $details['required'] == 1 ? 'Yes' : ''); ?></td><td><?php echo $details['description']; ?><?php echo (isset($details['example']) ? '<span style="display: block; margin: 6px 0 8px 0; border-top: 1px solid #aaa; color: #666; padding: 4px; font-size: 10px;"><b>Example:</b> ' . $details['example'] . '</span>' : ''); ?></td>
		</tr>

	<?php } ?>

</table>
<?php endif; ?>


<?php if ($RAML->action()->get('body')->get('application/json')->get('schema')->get('properties')->isArray()): ?>
<h3>JSON参数</h3>
<table>
	<thead>
		<tr>
			<td>参数</td><td>类型</td><td>是否必填</td><td>描述</td>
		</tr>
	</thead>

	<?php
	$tmp = $RAML->action()->get('body')->get('application/json')->get('schema')->toArray();
	if ($RAML->action()->get('body')->get('application/json')->get('schema')->isString()) {
		$tmp = json_decode($RAML->action()->get('body')->get('application/json')->get('schema')->toString(), true);
	}

	foreach ($tmp['properties'] as $param => $details) {
	?>

		<tr>
			<td><?php echo $param; ?></td><td><?php echo $details['type']; ?></td><td><?php echo (isset($details['required']) && $details['required'] == 1 ? 'Yes' : ''); ?></td><td><?php echo $details['description']; ?><?php echo (isset($details['example']) ? '<span style="display: block; margin: 6px 0 8px 0; border-top: 1px solid #aaa; color: #666; padding: 4px; font-size: 10px;"><b>Example:</b> ' . $details['example'] . '</span>' : ''); ?></td>
		</tr>

	<?php } ?>

</table>
<?php endif; ?>


<h3>代码示例</h3>
<a href="javascript:void(0);" onclick="switchCodeSample('codeSamplesJS','codeSampleLinkJS');" id="codeSampleLinkJS" class="codeSampleLink">JavaScript</a>
<a href="javascript:void(0);" onclick="switchCodeSample('codeSamplesPHP','codeSampleLinkPHP');" id="codeSampleLinkPHP" class="codeSampleLink">PHP</a>
<a href="javascript:void(0);" onclick="switchCodeSample('codeSamplesRails','codeSampleLinkRails');" id="codeSampleLinkRails" class="codeSampleLink">Rails</a>
<pre class="codeSample" id="codeSamplesJS"><code><?php echo str_replace("\n", "<br />", $codeSamples->javascript()); ?></code></pre>
<pre class="codeSample" id="codeSamplesPHP"><code><?php echo str_replace("\n", "<br />", $codeSamples->php()); ?></code></pre>
<pre class="codeSample" id="codeSamplesRails"><code><?php echo str_replace("\n", "<br />", $codeSamples->rails()); ?></code></pre>

<script language="javascript">
	$('#codeSampleLinkJS').addClass('activeCodeSample');
	$('.codeSample').hide();
	$('#codeSamplesJS').show();

	function switchCodeSample(view, source) {
		$('.codeSampleLink').removeClass('activeCodeSample');
		$('#' + source).addClass('activeCodeSample');
		$('.codeSample').hide();
		$('#' + view).show();
	}
</script>

<h3>响应结果</h3>

<?php foreach ($RAML->path()->getResponses() as $code => $responses) { ?>
	<tt><strong><?php echo $code; ?>:</strong></tt><br /><br />

	<?php
		foreach ($responses as $response) {
			if (in_array($response['type'], array('example', 'schema'))) {
				continue;
			}

			echo '<tt>' . (is_array($response['type']) ? array_shift($response['type']) : $response['type']) . '</tt>';

			if (isset($response['example'])):
	?>
	<div class="apiresponse">
			<?php echo formatResponse($response['example']); ?>
	</div>
	<?php endif; ?>
	<p>&nbsp;</p>
	<?php
		}
	} ?>



<?php if (count($RAML->path()->getVerbs()) > 1): ?>
	<h3>Other Endpoint Actions</h3>
	<?php
	foreach ($RAML->path()->getVerbs() as $verb) {
		if ($verb == $RAML->getCurrentAction()) {
			continue;
		}
	?>
	<div style="float: left;">
		<a href="<?php echo $_SERVER['PHP_SELF']; ?>?path=<?php echo $RAML->getCurrentPath(); ?>&action=<?php echo $verb; ?>" class="badge badge_<?php echo $verb; ?>"><?php echo $verb; ?></a>
	</div>
	<?php } ?>
<?php endif; ?>


<?php if ($tryIt) { ?>
<!-- TRY IT FUNCTIONALITY -->
<script type="text/JavaScript" language="JavaScript">
	function showTryIt() {
		$('.tryitmodal').show();
	}

	function hideTryIt() {
		$('.tryitmodal').hide();
	}

	function doTryIt() {
		xmlHttp = new XMLHttpRequest();
		tryItStop = false;

		$('#tryitform').submit();

		// Handle queryParameters
		tryItPath = '<?php echo $RAML->baseUri . $RAML->getCurrentPath(); ?>' + '?';
		$('.tryitparams').each(function(param) {
			if ($(this).val() != '') {
				tryItPath += $(this).attr('name') + '=' + escape($(this).val()) + '&';
				$(this).attr('style', 'width: 100%;');
			} else if ($(this).hasClass('tryitrequired')) {
				$(this).css('border', '1px solid #900').css('background', '#900').css('color', '#fff').focus();
				tryItStop = true;
			}
		});

		// Check for errors
		if (tryItStop) {
			return;
		}

		var tryItData = null;

		<?php if ($RAML->getCurrentAction() != 'GET') { ?>
			var tryItData = $('.tryitbody').val();
		<?php } ?>

		xmlHttp.onreadystatechange = function() {
            if (xmlHttp.readyState == 4) {
                var response = 'Response Code: ' + xmlHttp.status + "\nContent-type: " + xmlHttp.getResponseHeader('Content-type') + "\n----------------------\n\n" + xmlHttp.responseText;
                $('.tryitresult').val(response);

                hljs.configure({useBR: true});
				$('.tryitresult').each(function(i, block) {
				  hljs.highlightBlock(block);
				});
            }
        };
		xmlHttp.open("<?php echo $RAML->getCurrentAction(); ?>", tryItPath);

		// Add in Headers
		$('.tryitheaders').each(function(param) {
			if ($(this).val() != '') {
				var valueName = $(this).attr('name') + 'value';
				xmlHttp.setRequestHeader($(this).val(), $("input[name='"+valueName+"']").val());
			}
		});

		xmlHttp.send(tryItData);
	}
</script>
<div class="tryitmodal" style="display: none; position: absolute; top: 0; left: 0; height: 200%; width: 100%; background: #333; opacity: 0.8;"></div>
<div class="tryitmodal" style="display: none; position: absolute; top: 0; left: 0; right: 0; margin: 40px auto; width: 500px; padding: 20px; background: #fff;">
	<form id="tryitform" action="javascript:void(0);">
		<span class="badge badge_<?php echo $RAML->getCurrentAction(); ?>"><?php echo $RAML->getCurrentAction(); ?></span>
		<strong style="display: inline-block; font-size: 14px; padding-top: 5px;" class="text_<?php echo $RAML->getCurrentAction(); ?>"><?php echo $RAML->baseUri . $RAML->getCurrentPath(); ?></strong>
		<a href="javascript:void(0);" class="badge" style="background: #900; float: right; margin-right: 0;" onclick="hideTryIt();">CLOSE</a>
		<table>
		<tr><td colspan="2"><b>Headers</b><a href="javascript:void(0);" onclick="if ($('.tryitheaderrow').is(':visible')) { $('.tryitheaderrow').hide(); } else { $('.tryitheaderrow').show(); }" style="margin-top: 4px; font-size: 10px; float: right; color: #333; text-decoration: none;">show/hide</a></td></tr>
		<tr class="tryitheaderrow" style="display: none;"><td><input type="text" style="width: 100%;" name="tryitheader1" value="" class="tryitheaders"></td><td><input type="text" style="width: 100%;" name="tryitheader1value" value=""></td></tr>
		<tr class="tryitheaderrow" style="display: none;"><td><input type="text" style="width: 100%;" name="tryitheader2" value="" class="tryitheaders"></td><td><input type="text" style="width: 100%;" name="tryitheader2value" value=""></td></tr>
		<tr class="tryitheaderrow" style="display: none;"><td><input type="text" style="width: 100%;" name="tryitheader3" value="" class="tryitheaders"></td><td><input type="text" style="width: 100%;" name="tryitheader3value" value=""></td></tr>
		<tr class="tryitheaderrow" style="display: none;"><td><input type="text" style="width: 100%;" name="tryitheader4" value="" class="tryitheaders"></td><td><input type="text" style="width: 100%;" name="tryitheader4value" value=""></td></tr>
		<tr class="tryitheaderrow" style="display: none;"><td colspan="2">&nbsp;</td></tr>
		<tr><td colspan="2"><b>Query Parameters</b><a href="javascript:void(0);" onclick="if ($('.tryitparamrow').is(':visible')) { $('.tryitparamrow').hide(); } else { $('.tryitparamrow').show(); }" style="margin-top: 4px; font-size: 10px; float: right; color: #333; text-decoration: none;">show/hide</a></td></tr>
		<?php foreach ($RAML->action()->get('queryParameters')->toArray() as $param => $details) { ?>
		<tr class="tryitparamrow"><td><?php echo $param . (isset($details['required']) && $details['required'] == 1 ? '<span style="color: red">*</span>' : '' ); ?></td><td><input type="text" style="width: 100%;" name="<?php echo $param; ?>" value="<?php echo (isset($details['default']) ? $details['default'] : ''); ?>" class="tryitparams <?php echo (isset($details['required']) && $details['required'] == 1 ? 'tryitrequired' : '' ); ?>"></td></tr>
		<?php } if($RAML->getCurrentAction() != 'GET') { ?>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr><td colspan="2"><b>Body Data</b><br /><textarea class="tryitbody" style="width: 100%; height: 100px;"></textarea></td></tr>
		<?php } ?>
		<tr><td colspan="2">&nbsp;</td></tr>
		<tr><td colspan="2"><a href="javascript:void(0);" class="badge" style="width: 200px;" onclick="doTryIt();">TRY IT</a><span style="float: right;"><b>Response:</b></span><br /><textarea class="tryitresult" style="margin-top: 4px; width: 100%; height: 150px;"></textarea></td></tr>
		</table>
	</form>
</div>
<?php } ?>
